import java.net.DatagramPacket;
import java.net.DatagramSocket;
//22nd of may
public class NetworkRecieverThread extends Thread {
	SynchQueue syn;

	int p;
	DatagramSocket s;
	public NetworkRecieverThread(SynchQueue syn) {
		super();
		this.syn = syn;
		p = 1000;
		try{
		s = new DatagramSocket(p);
		System.out.println("The Local port is "+s.getLocalPort());
		System.out.println("The port is "+s.getPort());
		System.out.println("The  Send Buffer Size is "+s.getSendBufferSize());
		System.out.println("Datagram socket successfully created");
		}
		catch(Exception e){
		System.out.println(e);
		}
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(Runnable arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(ThreadGroup arg0, Runnable arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(String arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(ThreadGroup arg0, String arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(Runnable arg0, String arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(ThreadGroup arg0, Runnable arg1, String arg2) {
		super(arg0, arg1, arg2);
		// TODO Auto-generated constructor stub
	}

	public NetworkRecieverThread(ThreadGroup arg0, Runnable arg1, String arg2,
			long arg3) {
		super(arg0, arg1, arg2, arg3);
		// TODO Auto-generated constructor stub
	}

	public void run() {
		
		
		while (true) {
			DatagramPacket temp = recieve(p);
			if (temp != null)
				syn.enqueue(temp);
		}
	}

	private DatagramPacket recieve(int port) {
		byte[] message = new byte[NetworkSender.SIZE];
		try {
			
			DatagramPacket p = new DatagramPacket(message, message.length);
			//System.out.println("Got to here");
			s.receive(p);
			//System.out.println("Got to here Also");
			//message = p.getData();
			//System.out.println(message.length);
			//System.out.println("Message Recieved: "+message[0]+" "+message[1]+" "+message[2]);
			return p;
		} catch (Exception e) {
			System.out.println(e);
			return null;
		}



	}

}
